package com.xdemo.personal.fileupload;

import java.io.IOException;
import java.util.List;

import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileUpload;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.RequestContext;
import org.jboss.netty.handler.codec.http.HttpMethod;
import org.jboss.netty.handler.codec.http.HttpRequest;

public class NettyFileUpload extends FileUpload {
	
	private NettyRequestContext context;
	
	public NettyFileUpload(FileItemFactory fileItemFactory) {
		super(fileItemFactory);
	}

	@Override
	public List<?> parseRequest(RequestContext ctx) throws FileUploadException {
		return super.parseRequest(ctx);
	}

	public void setContext(NettyRequestContext context) {
		this.context = context;
	}
	
	public static boolean isMultipartContent(HttpRequest request) {
		if(request.getMethod() != HttpMethod.POST) {
			return false;
		}
		if(request.getHeaders("Content-Type") == null || request.getHeaders("Content-Type").size() == 0) {
			return false;
		}
		String _contentType = request.getHeaders("Content-Type").get(0);
		if(_contentType.startsWith("multipart/")) {
			return true;
		}
		return false;
	}

	public FileItemIterator getItemIterator() throws FileUploadException, IOException {
		return super.getItemIterator(context);
	}
	
}
